
import React from 'react';
import { ToolType } from '../constants/ToolType';
import Controller from "../Controller";

/** 快捷键 */
export default function useHotKey(controller: Controller) {

    const model = controller.useMemo(s => s.model, [])

    React.useEffect(() => {
        if (model === 'preview') return;

        function onkeydown(e: KeyboardEvent) {
            switch (e.code) {
                case 'KeyM':
                    controller.run(async s => { s.selectedTool = ToolType.移动 })
                    break;
                case 'KeyQ':
                    controller.run(async s => { s.selectedTool = ToolType.形变 })
                    break;
                case 'KeyR':
                    controller.run(async s => { s.selectedTool = ToolType.旋转 })
                    break;
                case 'KeyC':
                    controller.run(async s => { s.selectedTool = ToolType.摄像机 })
                    break;
                case 'KeyV':
                    controller.run(async s => { s.selectedTool = ToolType.选取 })
                    break;
                case 'KeyI':
                    controller.run(async s => { s.selectedTool = ToolType.模型 })
                    break;
            }
        }

        window.addEventListener('keydown', onkeydown);

        return () => {
            window.removeEventListener('keydown', onkeydown)
        }
    }, [controller, model])
}